package com.hoshino.momochi.controller;

import javax.servlet.http.HttpServletRequest;

import com.hoshino.momochi.annotation.IgnoreLogin;
import com.hoshino.momochi.annotation.LimitCount;
import com.hoshino.momochi.annotation.MaintainToken;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.model.DTO.LoginDTO;
import com.hoshino.momochi.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * TEST
 */
@Slf4j
@RestController
//@CrossOrigin
public class LoginController {

    @Autowired
    public AdminService adminService;

    @RequestMapping("/login")
    @LimitCount(key = "login", name = "登录接口", prefix = "limit")
    @IgnoreLogin
    public JsonResponse login(
            @RequestBody LoginDTO loginDTO, HttpServletRequest request) throws Exception {
        log.info("当前请求username:{},password:{}", loginDTO.getUsername(), loginDTO.getPassword());
        if (StringUtils.equals("张三", loginDTO.getUsername()) && StringUtils.equals("123456", loginDTO.getPassword())) {
            // 通过 数据库 操作 获取到了 用户 ID 并将其 保存
            // 保存到 request中 供 @After 方法 使用 USERID
            // token-id
            // LoginController->MaintainTokenAspect.SetTokenAndReturn

            return JsonResponse.success("HOSHINO");
        }
        return JsonResponse.success("账户名或密码错误");
    }

    @GetMapping("/maintain")
    @IgnoreLogin
    @MaintainToken
    public JsonResponse maintainToken() {
        // 维持 Token
        return JsonResponse.success("");
    }

}